﻿using System;
using System.ComponentModel;
using System.IO.Ports;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Data;
using System.IO;

namespace serial_socket_wpf.ViewModule
{

    class Serial_view_module : INotifyPropertyChanged, IDisposable
    {
        private class Serial_view_module_command : ICommand
        {
            Action _act;

            public Serial_view_module_command(Action exec_process)
            {
                _act = exec_process;
            }


            public event EventHandler CanExecuteChanged;
            public bool CanExecute(object parameter)
            {
                return true;
            }
            public void Execute(object parameter)
            {
                _act();
            }

        }

        public SerialPort serial = null;

        private string _baudrate;
        private string _com = null;
        private bool _serial_opend = false;
        private string _btn_string = "open";
        private ICollectionView _serial_available;
        private string _label = "serial_";

        public string baudrate
        {
            get { return _baudrate; }
            set
            {
                if (value != _baudrate)
                {
                    _baudrate = value;
                    NotifyPropertyChanged("baudrate");
                }
            }
        }

        public string com
        {
            get 
            { 
                if(_com == null)
                {
                    _com = serial_available.CurrentItem.ToString();
                }
                return _com; 
            }
            set
            {
                if (value != _com)
                {
                    _com = value;
                    NotifyPropertyChanged("com");

                }
            }
        }

        public bool ctrl_enabled
        {
            get { return !_serial_opend; }
            set
            {
                if (value == _serial_opend)
                {
                    _serial_opend = !value;
                    NotifyPropertyChanged("ctrl_enabled");
                }
            }
        }

        public string btn_string
        {
            get { return _btn_string; }
            set
            {
                if (value != _btn_string)
                {
                    _btn_string = value;
                    NotifyPropertyChanged("btn_string");
                }
            }
        }

        public ICollectionView serial_available
        {
            get
            {
                if (_serial_available == null)
                {
                    string[] _serials = System.IO.Ports.SerialPort.GetPortNames();
                    IList<string> list = new List<string>();

                    foreach (string s in _serials)
                    {
                        list.Add(s);
                    }
                    _serial_available = new CollectionView(list);
                }
                return _serial_available;
            }
        }

        public string label
        {
            get { return _label; }

            set
            {
                if (value != _label)
                {
                    _label = value;
                    NotifyPropertyChanged("label");
                }
            }
        }

        //public string[] 
        #region ICommand
        private void on_click()
        {
            if (_serial_opend)
            {
                serial.Close();
                ctrl_enabled = true;
                label = "closed";
                btn_string = "open";
            }
            else
            {
                try
                {
                    serial.PortName = _com;
                    serial.BaudRate = int.Parse(_baudrate);
                    serial.Open();
                    ctrl_enabled = false;
                    label = "opend";
                    btn_string = "close";

                }
                catch(SystemException se)
                {
                    MessageBox.Show(se.Message);
                }
            }
        }

        private ICommand _Command_on_click = null;
        public ICommand OnClickCommand
        {
            get
            {
                if (_Command_on_click == null)
                {
                    _Command_on_click = new Serial_view_module_command(on_click);
                }
                return _Command_on_click;
            }
        }

        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        #region IDispose
        private bool _disposed = false;
        ~Serial_view_module()
        {
            Dispose(false);
        }
        public Serial_view_module()
        {
            serial = new SerialPort();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                // 清理托管资源
            }

            //清理托管资源
            if (serial != null)
            {
                serial.Dispose();
                serial = null;
            }
            _disposed = true;
        }
        #endregion
    }
}
